package com.chuangjiangx.member.coupon.ddd.domain.model;

import java.util.ArrayList;
import java.util.List;

/**
 * 会员权益展示枚举
 *
 * @author wp
 * @create 2018-08-28 17:33
 **/
public enum ShowEquityEnum {
    AMOUNT("余额", 1),
    SCORE("积分", 2),
    COUPON("卡券", 3);

    public String name;
    public Integer code;

    ShowEquityEnum(String name, Integer code) {
        this.name = name;
        this.code = code;
    }

    /**
     * 根据code获取对象
     *
     * @param code
     * @return
     */
    public static ShowEquityEnum fromCode(Integer code) {
        for (ShowEquityEnum showEquityEnum : ShowEquityEnum.values()) {
            if (code.equals(showEquityEnum.code)) {
                return showEquityEnum;
            }
        }
        throw new IllegalStateException("不存在 该选项:  " + code);
    }

    /**
     * jsonString 转list
     *
     * @param param
     * @return
     */
    public static List<ShowEquityEnum> fromCodeList(String param) {
        String[] codeList = param.split(",");
        List<ShowEquityEnum> showEquityEnumList = new ArrayList<>();
        for(String code:codeList){
            ShowEquityEnum showEquityEnum = fromCode(Integer.valueOf(code));
            showEquityEnumList.add(showEquityEnum);
        }
        return showEquityEnumList;
    }

    /**
     * list 转jsonString
     *
     * @param showEquityEnumList
     * @return
     */
    public static String toJsonList(List<ShowEquityEnum> showEquityEnumList) {
        StringBuilder codeList = new StringBuilder();
        showEquityEnumList.forEach(showEquityEnum -> {
            codeList.append(showEquityEnum.code);
            codeList.append(",");
        });
        if(codeList.length()>0){
            codeList.deleteCharAt(codeList.lastIndexOf(","));
        }
        return codeList.toString();
    }
}
